#!/usr/bin/env python3

from launch import LaunchDescription
from launch_ros.actions import ComposableNodeContainer, Node
from launch_ros.descriptions import ComposableNode
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch.conditions import IfCondition, UnlessCondition

def generate_launch_description():
    # 声明启动参数
    use_component_arg = DeclareLaunchArgument(
        'use_component',
        default_value='true',
        description='Whether to use component mode'
    )
    
    # 组件容器 - 在同一进程中运行多个组件
    component_container = ComposableNodeContainer(
        name='component_container',
        namespace='',
        package='rclcpp_components',
        executable='component_container',
        composable_node_descriptions=[
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentPublisher',
                name='component_publisher',
                parameters=[{
                    'publish_frequency': 2.0,
                    'message_prefix': 'Component'
                }]
            ),
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentSubscriber',
                name='component_subscriber',
                parameters=[{
                    'node_name': 'component_subscriber'
                }]
            ),
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentParam',
                name='component_param',
                parameters=[{
                    'param_string': 'Component parameter value',
                    'param_int': 100,
                    'param_double': 2.718,
                    'param_bool': False
                }]
            )
        ],
        output='screen',
        condition=IfCondition(LaunchConfiguration('use_component'))
    )
    
    # 传统节点方式 - 每个节点独立进程
    traditional_nodes = [
        Node(
            package='multi_launch_demo',
            executable='publisher_node',
            name='traditional_publisher',
            parameters=[{
                'publish_frequency': 2.0,
                'message_prefix': 'Traditional'
            }],
            output='screen',
            condition=UnlessCondition(LaunchConfiguration('use_component'))
        ),
        Node(
            package='multi_launch_demo',
            executable='subscriber_node',
            name='traditional_subscriber',
            output='screen',
            condition=UnlessCondition(LaunchConfiguration('use_component'))
        ),
        Node(
            package='multi_launch_demo',
            executable='param_node',
            name='traditional_param',
            parameters=[{
                'param_string': 'Traditional parameter value',
                'param_int': 200,
                'param_double': 1.414,
                'param_bool': True
            }],
            output='screen',
            condition=UnlessCondition(LaunchConfiguration('use_component'))
        )
    ]
    
    return LaunchDescription([
        use_component_arg,
        component_container,
        *traditional_nodes
    ])
